package com.ruiysoft.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruiysoft.crm.constants.CrmConstants;
import com.ruiysoft.crm.mapper.BizBatchClientRecordMapper;
import com.ruiysoft.crm.model.entity.BizBatchClientRecord;
import com.ruiysoft.crm.model.entity.BizClient;
import com.ruiysoft.crm.service.BizBatchClientRecordService;
import com.ruiysoft.crm.service.BizClientService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fanyl
 */
@Service
public class BizBatchClientRecordServiceImpl extends ServiceImpl<BizBatchClientRecordMapper, BizBatchClientRecord> implements BizBatchClientRecordService {
    private final BizBatchClientRecordMapper bizBatchClientRecordMapper;

    public BizBatchClientRecordServiceImpl(BizBatchClientRecordMapper bizBatchClientRecordMapper) {
        this.bizBatchClientRecordMapper = bizBatchClientRecordMapper;
    }

    @Override
    public boolean insert(List<Integer> userIds, int userId, String type, String remark, String userName) {

        String batchNo = LocalDateTime.now().format(DateTimeFormatter.ofPattern(CrmConstants.TIME_NO_FUII_FORMAT)) + String.format("%05d", userId);
        BizBatchClientRecord bizBatchClientRecord = new BizBatchClientRecord();
        if (StringUtils.isNotBlank(remark)) {
            bizBatchClientRecord.setRemark(remark);
        }
        if (StringUtils.isNotBlank(type)) {
            bizBatchClientRecord.setBatchType(type);
        }
        bizBatchClientRecord.setCreateDate(LocalDateTime.now());
        bizBatchClientRecord.setBatchNo(batchNo);
        bizBatchClientRecord.setClientIds(StringUtils.join(userIds, ","));
        bizBatchClientRecord.setUserId(userId);
        bizBatchClientRecord.setUserName(userName);

        return bizBatchClientRecordMapper.insert(bizBatchClientRecord) > 0;
    }

    @Autowired
    BizClientService bizClientService;

    @Override
    public IPage batchNuFindDetail(Page feny, String batchNu) {
        BizBatchClientRecord bizBatchClientRecord =  this.getOne(new QueryWrapper<BizBatchClientRecord>().eq("batch_no", batchNu), false);
        if(bizBatchClientRecord != null) {
            String[] clientIds = bizBatchClientRecord.getClientIds().split(",");
            int start = (int)feny.getCurrent();
            int end = (int)feny.getSize();

            // 如果小于50条
            if(clientIds.length < 50) {
                start = 0;
                end = clientIds.length;
            } else {
                if(start == 1)
                    start = 0;
                else {
                    start *= end;
                    start -= 1;
                }
            }

            List list = new ArrayList();
            for(long i = start; i < (start + end); i++) {
                BizClient bizClient = bizClientService.getById(clientIds[(int)i]);
                if(bizClient != null)
                    list.add(bizClient);
            }

            feny.setTotal(clientIds.length);
            feny.setRecords(list);

            return feny;
        }

        return null;
    }
}